package com.kob.design.proxy.cglib_proxy;

import org.springframework.cglib.proxy.Enhancer;
import org.springframework.cglib.proxy.MethodInterceptor;
import org.springframework.cglib.proxy.MethodProxy;

import java.lang.reflect.Method;

/**
 * @Description: 代理对象工厂，获取代理对象
 * @Author: fyt
 * @Date: 2024/4/15 14:35
 **/
public class ProxyFactory implements MethodInterceptor {
    private SellStation sellStation = new SellStation();
    public SellStation getProxyObject(){
        //Enhancer,类似于JDK代理中的proxy类
        Enhancer enhancer = new Enhancer();
        //设置父类的字节码对象,设置代理类的父类
        enhancer.setSuperclass(SellStation.class);
        //设置回调函数
        enhancer.setCallback(this);
        //创建代理对象
        SellStation proxyObject = (SellStation) enhancer.create();
        return proxyObject;
    }

    public Object intercept(Object o, Method method, Object[] objects, MethodProxy methodProxy) throws Throwable {
        System.out.println("cglib代理");
        Object obj = method.invoke(sellStation, objects);
        return obj;
    }
}
